package net.chrisrichardson.ec2deployer.cluster

import net.chrisrichardson.ec2deployer.core.CloudManager
import net.chrisrichardson.ec2deployer.applications.TerracottaServer
import net.chrisrichardson.ec2deployer.core.EC2Server
import net.chrisrichardson.ec2deployer.util.FileBasedTemplate
import net.chrisrichardson.ec2deployer.applications.CatalinaOptionsBuilder
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory


class TerracottaTier extends AbstractTier {

    Log logger = LogFactory.getLog(getClass())

    ClusterSpec spec
    List<TerracottaServer> terracottaServers

    TerracottaTier() {

    }
    
    TerracottaTier(CloudManager ec2, spec) {
       super(ec2)
        this.spec = spec
    }


    def saveState(result) {
        if (!terracottaServers.empty)
            result.put("terracottaServers", terracottaServers.host.instanceId)
    }

    def restoreState(serverState) {
        int serverId = 0
        this.terracottaServers = serverState["terracottaServers"].collect {
            new TerracottaServer(ec2.findInstance(it), serverId++)
        }

    }


    public assignHosts(Object hosts) {
        terracottaServers = []
        def hostIt = hosts.terracottaHosts.iterator()
        def serverId = 0
        spec.numberOfTerracottaHosts.times {terracottaServers.add(new TerracottaServer(hostIt.next(), serverId++)) }
    }

    void performStart() {
        logger.debug("Starting TC servers")
        writeConfigurationFile()
        terracottaServers*.startTerracotta()
    }

    public void peformStop() {
        logger.debug("Stopping TC servers")
        terracottaServers*.stopTerracotta()
    }

    void restart() {
        stop()
        start()
    }

    def writeConfigurationFile() {
        for (tcServer in terracottaServers) {
            writeConfigurationFile tcServer.host, "/root/tc-config.xml"
        }
    }

    def writeConfigurationFile(EC2Server host, file) {
        FileBasedTemplate template = new FileBasedTemplate()
        def text = template.process("/templates/tc-config.xml", [servers: terracottaServers.host])
        host.ssh("cat > $file", text)
    }

    def prepareClient(EC2Server host) {
        if (spec.numberOfTerracottaHosts > 0) {
            logger.debug("preparing TC client")
            writeConfigurationFile host, "/var/lib/tomcat5/tc-config.xml"
            installDsoBootJar(host)
        }
    }

    def installDsoBootJar(EC2Server host) {
         host.ssh "export JAVA_HOME=/usr/java/default; cd /var/lib/tomcat5 ; /var/lib/terracotta-2.5.4/bin/make-boot-jar.sh -o dso-boot.jar"
    }

    def addToCatalinaOpts(CatalinaOptionsBuilder optionsBuilder) {
        if (spec.numberOfTerracottaHosts > 0) {
            optionsBuilder.arg("-Xbootclasspath/p:/var/lib/tomcat5/dso-boot.jar")
            optionsBuilder.prop("tc.install-root", TerracottaServer.TC_INSTALL_ROOT)
            optionsBuilder.prop("tc.config", "/var/lib/tomcat5/tc-config.xml")
        }
    }

    def getSarMonitors() {
        terracottaServers.sarMonitor
    }

    List<EC2Server> getServers() {
        terracottaServers.host
    }

}